package wnhz.com.business.shareshop.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.TextView;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import wnhz.com.business.MyApplication;
import wnhz.com.business.R;
import wnhz.com.business.base.BaseActivity;
import wnhz.com.business.groupshop.bean.InfoBean;
import wnhz.com.business.impl.ActionBarClickListener;
import wnhz.com.business.netcallback.DialogCallback;
import wnhz.com.business.uitls.Base64Util;
import wnhz.com.business.uitls.CountDownTimerUtils;
import wnhz.com.business.uitls.LogUtils;
import wnhz.com.business.uitls.MyCallBack;
import wnhz.com.business.uitls.Url;
import wnhz.com.business.uitls.XUtil;
import wnhz.com.business.view.TranslucentActionBar;

/**
 * 验证添加银行卡
 */
public class YanZhengCardActivityS extends BaseActivity implements ActionBarClickListener {

    @BindView(R.id.actionbar)
    TranslucentActionBar actionbar;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_cardnumber)
    EditText etCardnumber;
    @BindView(R.id.tv_getyzm)
    TextView tvGetyzm;
    @BindView(R.id.tv_ok)
    TextView tvOk;
private String name,bank_num,bank_name;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_yan_zheng_cards);
        ButterKnife.bind(this);
if(null!=getIntent()){
    name = getIntent().getStringExtra("name");
    bank_num = getIntent().getStringExtra("bank_num");
    bank_name = getIntent().getStringExtra("bank_name");
}
        actionbar.setData("添加银行卡验证", R.drawable.ic_lefft_white, null, 0, null, 0, this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            actionbar.setStatusBarHeight(getStatusBarHeight());
        }
        etCardnumber.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (etCardnumber.getText().toString().equals("")||etCardnumber.getText().toString()==null){
                    tvOk.setTextColor(getResources().getColor(R.color.text51));
                    tvOk.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_circle_grayd));
                    tvOk.setOnClickListener(null);
                }else
                {
                    tvOk.setTextColor(getResources().getColor(R.color.white));
                    tvOk.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_circle_reds));
                    tvOk.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                                    if (etPhone.getText().toString().equals("") || etPhone.getText().toString() == null &&
                                            etCardnumber.getText().toString().equals("") || etCardnumber.getText().toString() == null) {
                                        MyToast("请完善信息");
                                    } else {
                                        bindCard();
                                    }
                        }
                    });
                }
            }
        });
    }

    public static Intent createIntent(Context context) {
        Intent intent = new Intent(context, YanZhengCardActivityS.class);
        return intent;
    }

    @Override
    public void onLeftClick() {
        finish();

    }

    @Override
    public void onRightClick() {

    }

    @Override
    public void onEditTextClick() {

    }

    @OnClick({R.id.tv_getyzm})
    public void Click(View v) {
        switch (v.getId()) {
            case R.id.tv_getyzm:
                CountDownTimerUtils mCountDownTimerUtils = new CountDownTimerUtils(tvGetyzm, 60000, 1000);
                mCountDownTimerUtils.start();
                getLoginCode();
                break;
        }
    }

    private void bindCard() {//绑定银行卡
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getInfoBean().getToken());
        map.put("name", name);//持卡人姓名
        map.put("bank_num", bank_num);//银行卡号
        map.put("bank_name", bank_name);//银行卡类型
        map.put("mobile",Base64Util.encodedString2(etPhone.getText().toString()));//手机号
        map.put("code",etCardnumber.getText().toString());//验证码
        XUtil.Post(Url.UCENTER_ADD_BANK, map, new MyCallBack<String>() {
            @Override
            public void onFinished() {
                super.onFinished();
            }

            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    LogUtils.e(" ==== 收藏 ==", result);
                    if (jsonObject.optString("re").equals("1")) {
                        MyToast(jsonObject.getString("info"));
                        finish();
                    }else {
                        MyToast(jsonObject.getString("info"));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
    private void getLoginCode() {
        //    获取验证码
        //    @param Str    code      接口请求密码  UNOHACHA
        //    @param Int    mobile    手机号   （加密）
        //    @param Int    type      1 注册  2 登录  3 忘记密码   4 更换手机号  5 修改交易密码  6 修改登录密码  7三方登陆  8绑定银行卡
        //    syb.unohacha.com/Api/Api/User_getMessage
        Map<String,String> map = new HashMap<>();
        map.put("code", Url.CODE);
        map.put("mobile", Base64Util.encodedString2(etPhone.getText().toString()));
        map.put("type", "8");
        Log.e("获取验证码", "" + Url.REGISTER_GETCODE);
        OkGo.<InfoBean>post(Url.REGISTER_GETCODE)
                .tag(this)
                .params(map,false)
                .execute(new DialogCallback<InfoBean>(this) {
                    @Override
                    public void onSuccess(Response<InfoBean> response) {
                        Log.e("REGISTER_GETCODE", "" + response);
//                        if (response.body().getRe().equals("1")) {
                        if(!TextUtils.isEmpty(response.body().getInfo())){
                            MyToast(response.body().getInfo());
                        }
//                        }
                    }

                    @Override
                    public void onFinish() {
                        super.onFinish();
                    }

                    @Override
                    public void onError(Response<InfoBean> response) {
                        super.onError(response);
                    }
                });
    }
}
